package com.zhentao.datacontrol.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zhentao.datacontrol.domain.entity.ParameterTable;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface ParameterTableMapper extends BaseMapper<ParameterTable> {

    /**
     * 查询所有启用的参数表
     */
    @Select("SELECT * FROM dc_parameter_table WHERE status = 'ACTIVE' AND is_delete = 0 ORDER BY table_name")
    List<ParameterTable> selectActiveParameterTables();

    /**
     * 根据表类型查询参数表
     */
    @Select("SELECT * FROM dc_parameter_table WHERE table_type = #{tableType} AND status = 'ACTIVE' AND is_delete = 0 ORDER BY table_name")
    List<ParameterTable> selectByTableType(@Param("tableType") String tableType);

    /**
     * 根据表编码查询参数表
     */
    @Select("SELECT * FROM dc_parameter_table WHERE table_code = #{tableCode} AND is_delete = 0")
    ParameterTable selectByTableCode(@Param("tableCode") String tableCode);

    /**
     * 检查表编码是否存在
     */
    @Select("SELECT COUNT(*) FROM dc_parameter_table WHERE table_code = #{tableCode} AND is_delete = 0 AND (#{id} IS NULL OR id != #{id})")
    int checkTableCodeExists(@Param("tableCode") String tableCode, @Param("id") Long id);
}
